package com.browsesoft.user;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormModel;

public class UserModifyModel implements HTMLFormModel {
	/**
	 * 对应的用户
	 */
	User user = null;

	/**
	 * 输入的老密码
	 */

	String oldpassword = "";

	/**
	 * 初始化model
	 * 
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	public void updateAttributes(Hashtable ht) throws Exception {
		user.getAttributes().putAll(ht);
	}

	/**
	 * 得到属性
	 * 
	 * @return 存储属性的哈息表
	 */
	public Hashtable getAttributes() throws Exception {
		return user.getAttributes();
	}

	/**
	 * 得到选择列表
	 * 
	 * @param name
	 *            名字
	 * @return 列表
	 */
	public Iterator getOptions(String name) throws Exception {
		return new LinkedList().iterator();
	}

	/**
	 * 处理用户请求
	 * 
	 * @param request
	 *            客户端请求
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到请求用户名
		if (user == null) {
			user = (User) request.getSession().getAttribute("loginUser");
		}
		if (user != null) {
			// 得到修改密码的叁数
			String oldP = request.getParameter("oldPassword");
			oldP = EncryptTools.getEncryptDataByRight(oldP, "userpassword");
			String newP = request.getParameter("newPassword");
			String affirmP = request.getParameter("affirmPassword");
			oldpassword = oldP;
			// e.setAttribute("onload", "passwordupdate()");
			if ((oldP != null) && (newP != null) && (affirmP != null)) {
				// 加密
				String item = "userpassword";
				newP = EncryptTools.getEncryptDataByRight(newP, item);
				affirmP = EncryptTools.getEncryptDataByRight(affirmP, item);
				// 修改密码
				user.modifyPassword(oldP, newP, affirmP);
			}
		}
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}
}